package com.example.activiti.dataflow;

import com.example.activiti.common.utils.StringUtils;
import com.example.activiti.dataflow.enums.DataFlowTransformType;
import lombok.extern.slf4j.Slf4j;
import org.activiti.bpmn.model.FieldExtension;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.ServiceTask;
import org.activiti.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Slf4j
@Service
public class DataFlowTaskService {

    private Map<String, List<Map<String, Object>>> flowDataListMap = new HashMap<>();

    public void transform(DelegateExecution delegateExecution) {
        FlowElement currentFlowElement = delegateExecution.getCurrentFlowElement();
        if (currentFlowElement instanceof ServiceTask) {
            ServiceTask serviceTask = (ServiceTask) currentFlowElement;
            String resultVariableName = serviceTask.getResultVariableName();
            List<FieldExtension> fieldExtensions = serviceTask.getFieldExtensions();
            if (fieldExtensions.size() > 0) {
                String fieldName = fieldExtensions.get(0).getFieldName();
                String fieldVale = fieldExtensions.get(0).getStringValue();
                dealFlowDataByType(delegateExecution, fieldVale, resultVariableName);
            }
        }
    }

    private void dealFlowDataByType(DelegateExecution delegateExecution, String fieldVale, String resultVariableName) {
        if (StringUtils.isNotEmpty(fieldVale)) {
            if (fieldVale.equals(DataFlowTransformType.SUM.getType())) {
                log.info("进行求和运算");
                delegateExecution.setVariable(resultVariableName + "Res", 9);
                Map<String, Object> dataFlowMap = new HashMap<>(2);
                dataFlowMap.put(resultVariableName, 1024);
                List<Map<String, Object>> flowDataList = Collections.singletonList(dataFlowMap);
                flowDataListMap.put(delegateExecution.getProcessInstanceId() + "_" + fieldVale, flowDataList);
            } else if (fieldVale.equals(DataFlowTransformType.FILTER.getType())) {
                log.info("进行筛选运算");
                delegateExecution.setVariable(resultVariableName + "Res", 999);
                Map<String, Object> dataFlowMap = new HashMap<>(2);
                dataFlowMap.put(resultVariableName, 999);
                List<Map<String, Object>> flowDataList = Collections.singletonList(dataFlowMap);
                flowDataListMap.put(delegateExecution.getProcessInstanceId() + "_" + fieldVale, flowDataList);
            }
        } else {
            log.info("transformType is NULL!");
        }
    }

    /**
     * 根据Key清理数据流缓存数据
     *
     * @param processInstanceBusinessKey 数据流缓存的Key值
     */
    public void clearFlowDataByProcessInstanceBusinessKey(String processInstanceBusinessKey) {
        flowDataListMap.remove(processInstanceBusinessKey);
    }

}
